from just_pipeline.pipeline import Pipeline, pipe_last, pipeline, pipe
from just_pipeline.immutable import immutable_args
from just_pipeline.cases import Case


def concat(ls: list, other: list):
    return ls + other


@immutable_args
def sort(ls: list):
    ls.sort()
    return ls


def default_style():
    ls = [1, 2, 3]
    t = ls + [4] + [7] + [5, 6]
    t.sort()
    return t


def classic_style():
    ls = [1, 2, 3]
    return pipeline(ls)(
        pipe(concat, [4]),
        pipe(concat, [7]),
        pipe(concat, [5, 6]),
        pipe(sort),
    )


def pythonic_style():
    ls = [1, 2, 3]
    return Pipeline(ls)[
        pipe(concat, [4]),
        pipe(concat, [7]),
        pipe(concat, [5, 6]),
        pipe(sort),
    ].run()


def f1():
    return 6


def f2(n):
    return n


def f3(n):
    return n


def f4(n):
    return n


def main():
    same = default_style() == classic_style() == pythonic_style()
    print(f"结果相等={same}")

    with_cases = pipeline(
        data=[1, 2, 3, 4],
        cases=[
            # 先将data列表转为字符串列表
            Case(when=lambda x: len(x) == 4, then=[pipe_last(map, str), pipe(list)]),
            # 如果列表长度大于4，则将所有字符串元素末尾加上"+10"
            Case(
                when=lambda x: len(x) > 4,
                then=[
                    pipe_last(filter, lambda x: isinstance(x, str)),
                    pipe_last(map, lambda x: str(x) + "+10"),
                    pipe(list),
                ],
            ),
        ],
    )(pipe(concat, [5, 6]), pipe(list))
    print("with_cases: ", with_cases)
